<template>
  <div>
    <!-- 2.6使用status渲染 -->
    <el-dialog
      :title="status.isAdd ? '管理员添加' : '管理员编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <!-- 6.4 绑定数据到表单 -->
      <el-form :model="form">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex';
import { requseradd, requseredit, requserinfo, reqrolelist } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  components: {},
  props: ["status", "list"],
  data() {
    return {
        roleList:[],
      //初始化表单数据
      form: {
        roleid:"",
        username:"",
        password:"",
        status:1
      },
    };
  },
  methods: {
    ...mapActions(["changeInfo"]),
    //点击了取消
    cancel() {
      this.status.isshow = false;
    },
      //6.6form数据清空
    empty() {
      this.form = {
        roleid:"",
        username:"",
        password:"",
        status:1
      };
    },
    //6.5添加逻辑
    add() {
      //发请求
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //form的数据清空一下
          this.empty();
          //弹框消失
          this.status.isshow = false;
          //列表数据重新渲染
          this.$emit("init");
        }
      });
    },
    //7.3点击了编辑按钮，获取一条数据
    getOne(uid) {
      requserinfo({
        uid: uid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          //密码
          this.form.password= "";
        }
      });
    },
    //8.1修改逻辑
    update() {
      requseredit(this.form).then((res) => {
        if (res.data.code == 200) {
          // 1.弹成功
          successAlert(res.data.msg);

          //验证之前修改的角色是不是当前登录用户所属的角色，如果是，就要清除用户信息并退出
          if (this.form.uid === this.info.uid) {
            //清除数据
            this.changeInfo({})
            //退出登录
            this.$router.replace("/login")
            return;
          }
          // 2.form的数据清空一下
          this.empty();
          // 3.弹框消失
          this.status.isshow = false;
          // 4.列表需要重新渲染
          this.$emit("init");
        }
      });
    },
    //9.弹框关闭
    closed() {
      //如果是添加的取消，什么都不做；如果是编辑的取消，form清空
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
      //请求角色列表数据
      reqrolelist().then((res)=>{
          if(res.data.code==200){
              this.roleList = res.data.list
          }
      })
  },
  computed: {
    ...mapGetters(["info"])
  },
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>